package com.aaa.product.controller;


import com.aaa.product.entity.SeckillStatus;
import com.aaa.product.entity.TbActInf;
import com.aaa.product.entity.TbActProduct;
import com.aaa.product.entity.TbOrdInf;
import com.aaa.product.service.TbActInfService;
import com.aaa.product.service.TbActProductService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.List;

@RestController
@RequestMapping("seckill")
public class SeckillController {

    @Resource
    TbActInfService tbActInfService;
    @Resource
    TbActProductService tbActProductService;
    @Resource
    RedisTemplate redisTemplate;


    /**
     * 获取所有进行的活动
     * @param time
     * @return
     */
    @PostMapping("/getNowTimeActivity")
    public List<TbActInf> list(String time){
        List<TbActInf> tbActInfs = tbActInfService.selectTbActInfByNowTime(time);
        return tbActInfs;
    }
    /**
     * 获取一个进行的活动详情
     * @param time
     * @return
     */
    @PostMapping("/selectTbActInfByNowTimeAndTid")
    public TbActInf selectTbActInfByNowTimeAndTid(String time,String tid){
        TbActInf tbActInfs = tbActInfService.selectTbActInfByNowTimeAndTid(time,tid);
        return tbActInfs;
    }
    /**
     * 获取一个进行的活动详情中的某个商品的详情
     * @param time
     * @return
     */
    @PostMapping("/selectTbActInfByNowTimeAndPid")
    public TbActProduct selectTbActInfByNowTimeAndPid(String time, String pid){
        TbActProduct tbActProduct = tbActProductService.selectTbActInfByNowTimeAndPid(time, pid);
        return tbActProduct;
    }
    /**
     * 根据 用户名 查询订单状态
     */
    @PostMapping("/selectSeckillStatusByUsername")
    public Object selectSeckillStatusByUsername(String username){
        JSONObject jsonObject = new JSONObject();
        SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundHashOps("UserQueueStatus").get(username);
        System.out.println("多线程之后的订单数据");
        System.out.println(seckillStatus);
        if(seckillStatus != null){
            jsonObject.put("code",1);
            jsonObject.put("data",seckillStatus);
            jsonObject.put("msg","查询订单状态成功");
            return  jsonObject;
        }
        seckillStatus.setStatus(4);
        jsonObject.put("code",0);
        jsonObject.put("data",seckillStatus);
        jsonObject.put("msg","抢单失败");
        return jsonObject;
    }
    @PostMapping("/clearUserOrder")
    public void clearUserOrder(String username){
        List<TbActProduct> list = tbActProductService.list();
        for (TbActProduct tbActProduct : list){
            TbOrdInf tbOrdInfs = (TbOrdInf) redisTemplate.boundHashOps("UserOrder").get(tbActProduct.getTid()+username);
            if(null != tbOrdInfs){
                redisTemplate.boundHashOps("UserOrder").delete(tbActProduct.getTid()+username);
            }
        }

    }


}
